using System;
using System.Collections;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace DABL
{
    public static class QuestionnaireController
    {

        public static string QuestionnairesAdd(Questionnaire.qst_QuestionnairesRow QuestionnairesRow)
        {
            Database _db = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = _db.GetStoredProcCommand("QST_QuestionnairesAdd");
            _db.AddInParameter(dbCommand, "@QuestionnaireName", DbType.String, QuestionnairesRow.QuestionnaireName);
            _db.AddInParameter(dbCommand, "@StartDate", DbType.Date, QuestionnairesRow.StartDate);
            _db.AddInParameter(dbCommand, "@EndDate", DbType.Date, QuestionnairesRow.EndDate);
            _db.AddInParameter(dbCommand, "@ArchiveText", DbType.String, QuestionnairesRow.ArchiveText);
            _db.AddInParameter(dbCommand, "@LeadingText", DbType.String, QuestionnairesRow.LeadingText);
            _db.AddInParameter(dbCommand, "@AccountID", DbType.String, QuestionnairesRow.AccountID_FK);

            return  Convert.ToString(_db.ExecuteScalar(dbCommand));
        }
        public static DataSet QuestionnairesGetByID(string QuestionnaireID)
        {
            Database _db = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = _db.GetStoredProcCommand("QST_QuestionnairesGetByID");
            _db.AddInParameter(dbCommand, "@QuestionnaireID", DbType.String, QuestionnaireID);
            return _db.ExecuteDataSet(dbCommand);
        }
        public static DataSet QuestionnairesGet()
        {
            Database _db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _db.GetStoredProcCommand("QST_QuestionnairesGet");
            return _db.ExecuteDataSet(dbCommand);
        }

        public static IDataReader QuestionnairesGetReader()
        {
            Database _db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _db.GetStoredProcCommand("QST_QuestionnairesGet");
            return _db.ExecuteReader(dbCommand);
        }

        public static DataSet qst_QuestionnairesGetByAccountID(string AccountID_FK)
        {
            Database _db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _db.GetStoredProcCommand("qst_QuestionnairesGetByAccountID");
            _db.AddInParameter(dbCommand, "@AccountID_FK", DbType.String, AccountID_FK);
            
            return _db.ExecuteDataSet(dbCommand);
        }
        public static Int32 QuestionsAdd(Questionnaire.QST_QuestionsRow objQuestionsRow)
        {
            
            
            Database _db = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = _db.GetStoredProcCommand("QST_QuestionsAdd");
            _db.AddInParameter(dbCommand, "@QuestionType_FK", DbType.Int32, objQuestionsRow.QuestionType_FK);
            _db.AddInParameter(dbCommand, "@Question", DbType.String, objQuestionsRow.Question);
            _db.AddInParameter(dbCommand, "@QuestionnaireID_FK", DbType.Guid, objQuestionsRow.QuestionnaireID_FK);
            _db.AddInParameter(dbCommand, "@UpperText", DbType.String, objQuestionsRow.UpperText);
            _db.AddInParameter(dbCommand, "@LowerText", DbType.String, objQuestionsRow.LowerText);
            _db.AddInParameter(dbCommand, "@QuestionOrder", DbType.Int32, objQuestionsRow.QuestionOrder);
            return Convert.ToInt32(_db.ExecuteScalar(dbCommand));
        }
        public static Int32 QuestionsUpdate(Questionnaire.QST_QuestionsRow objQuestionsRow)
        {


            Database _db = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = _db.GetStoredProcCommand("QST_QuestionsUpdate");
            _db.AddInParameter(dbCommand, "@QuestionType_FK", DbType.Int32, objQuestionsRow.QuestionType_FK);
            _db.AddInParameter(dbCommand, "@Question", DbType.String, objQuestionsRow.Question);
            _db.AddInParameter(dbCommand, "@QuestionnaireID_FK", DbType.Guid, objQuestionsRow.QuestionnaireID_FK);
            _db.AddInParameter(dbCommand, "@UpperText", DbType.String, objQuestionsRow.UpperText);
            _db.AddInParameter(dbCommand, "@LowerText", DbType.String, objQuestionsRow.LowerText);
            _db.AddInParameter(dbCommand, "@QuestionID", DbType.Int32, objQuestionsRow.QuestionID);
            _db.AddInParameter(dbCommand, "@QuestionOrder", DbType.Int32, objQuestionsRow.QuestionOrder);
            return _db.ExecuteNonQuery(dbCommand);
        }


        public static Int32 QuestionsDelete(Int32 QuestionID)
        {

            Database _db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _db.GetStoredProcCommand("QST_QuestionsDelete");
            _db.AddInParameter(dbCommand, "@QuestionID", DbType.Int32, QuestionID);
            return _db.ExecuteNonQuery(dbCommand); 
        }
        public static Int32 OptionsDelete(Int32 OptionID)
        {

            Database _db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _db.GetStoredProcCommand("QST_OptionsDelete");
            _db.AddInParameter(dbCommand, "@OptionID", DbType.Int32, OptionID);
            return _db.ExecuteNonQuery(dbCommand);
        }


        public static DataSet QuestionTypesGet()
        {
            Database _db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _db.GetStoredProcCommand("QST_QuestionsTypesGet");
            return _db.ExecuteDataSet(dbCommand);
        }
        public static DataSet QuestionsGetByQuestionnaireID(string QuestionnaireID)
        {
            Database _db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _db.GetStoredProcCommand("QST_QuestionsGetByQuestionnaireID");
            _db.AddInParameter(dbCommand, "@QuestionnaireID", DbType.String, QuestionnaireID);
            return _db.ExecuteDataSet(dbCommand);
            
        }

        public static DataSet OptionsGetByQuestionID(Int32 QuestionID_FK)
        {
            
            Database _db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _db.GetStoredProcCommand("QST_OptionsGetByQuestionID");
            _db.AddInParameter(dbCommand, "@QuestionID_FK", DbType.Int32, QuestionID_FK);
            return _db.ExecuteDataSet(dbCommand);
        }
        public static Int32 OptionsAdd(Questionnaire.QST_OptionsRow OptionsRow)
        {


            Database _db = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = _db.GetStoredProcCommand("QST_OptionsAdd");
            _db.AddInParameter(dbCommand, "@QuestionID_FK", DbType.Int32, OptionsRow.QuestionID_FK);
            _db.AddInParameter(dbCommand, "@OptionText", DbType.String, OptionsRow.OptionText);
            return Convert.ToInt32(_db.ExecuteScalar(dbCommand));
        }

        public static Int32 OptionsUpdate(Questionnaire.QST_OptionsRow OptionsRow)
        {


            Database _db = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = _db.GetStoredProcCommand("Qst_OptionsUpdate");
            _db.AddInParameter(dbCommand, "@QuestionID_FK", DbType.Int32, OptionsRow.QuestionID_FK);
            _db.AddInParameter(dbCommand, "@OptionText", DbType.String, OptionsRow.OptionText);
            _db.AddInParameter(dbCommand, "@OptionID", DbType.String, OptionsRow.OptionID);
            return _db.ExecuteNonQuery(dbCommand);
        }

        public static Int32 AnswersAdd(Questionnaire.QST_AnswersRow AnswersRow)
        {


            Database _db = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = _db.GetStoredProcCommand("qst_AnswersAdd");
            _db.AddInParameter(dbCommand, "@Answer", DbType.String, AnswersRow.Answer);
            _db.AddInParameter(dbCommand, "@QuestionID_FK", DbType.Int32, AnswersRow.QuestionID_FK);
            _db.AddInParameter(dbCommand, "@OptionID_FK", DbType.Int32, AnswersRow.OptionID_FK);
            return _db.ExecuteNonQuery(dbCommand);
        }
        public static IDataReader UserAccountsGet(string UserID_FK)
        {
            Database _db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _db.GetStoredProcCommand("qst_UserAccountsGet");
            _db.AddInParameter(dbCommand, "@UserID_FK", DbType.String, UserID_FK);
            return _db.ExecuteReader(dbCommand);

        }
        public static int UserAccountsAdd(string UserID_FK, Int32 AccountID_FK)
        {
            Database _db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _db.GetStoredProcCommand("qst_UserAccountsAdd");
            _db.AddInParameter(dbCommand, "@UserID_FK", DbType.String, UserID_FK);
            _db.AddInParameter(dbCommand, "@AccountID_FK", DbType.Int32, AccountID_FK);
            return _db.ExecuteNonQuery(dbCommand);

        }
        public static int UserAccountsRemove(string UserID_FK, Int32 AccountID_FK)
        {
            Database _db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _db.GetStoredProcCommand("qst_UserAccountsRemove");
            _db.AddInParameter(dbCommand, "@UserID_FK", DbType.String, UserID_FK);
            _db.AddInParameter(dbCommand, "@AccountID_FK", DbType.Int32, AccountID_FK);
            return _db.ExecuteNonQuery(dbCommand);

        }
        
        public static Int32 AccountsAdd(string AccountName)
        {


            Database _db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _db.GetStoredProcCommand("qst_AccountsAdd");
            _db.AddInParameter(dbCommand, "@AccountName", DbType.String, AccountName);
            return _db.ExecuteNonQuery(dbCommand);
        }
        public static DataSet AccountsGetAll()
        {
            Database _db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _db.GetStoredProcCommand("qst_AccountsGetAll");
            return _db.ExecuteDataSet(dbCommand);
        }
        public static IDataReader AccountsGet()
        {
            Database _db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _db.GetStoredProcCommand("qst_AccountsGet");
            return _db.ExecuteReader(dbCommand);
        }
        public static Int32 AccountsRemove(Int32 AccountId)
        {

            Database _db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _db.GetStoredProcCommand("qst_AccountsRemove");
            _db.AddInParameter(dbCommand, "@AccountId", DbType.Int32, AccountId);
            return _db.ExecuteNonQuery(dbCommand);
        }
        public static DataSet AnswersLikertScaleResults(Int32 @QuestionID)
        {
            Database _db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _db.GetStoredProcCommand("qst_AnswersLikertScaleResults");
            _db.AddInParameter(dbCommand, "@QuestionID", DbType.Int32, @QuestionID);
            return _db.ExecuteDataSet(dbCommand);
        }

        public static DataSet AnswersMCQsResults(Int32 @QuestionID)
        {
            Database _db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _db.GetStoredProcCommand("qst_AnswersMCQsResults");
            _db.AddInParameter(dbCommand, "@QuestionID", DbType.Int32, @QuestionID);
            return _db.ExecuteDataSet(dbCommand);
        }
        public static DataSet AnswersOpendEndedResults(Int32 @QuestionID)
        {
            Database _db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _db.GetStoredProcCommand("qst_AnswersOpendEndedResults");
            _db.AddInParameter(dbCommand, "@QuestionID", DbType.Int32, @QuestionID);
            return _db.ExecuteDataSet(dbCommand);
        }
        

        
    }
}
